﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using YAPACS.RisServer.Abstractions;
using YAPACS.RisServer.Services.Dtos;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Controllers;
[Authorize]
public class RegisterController : AbpControllerBase
{
    private readonly IRegisterService _registerService;

    public RegisterController(IRegisterService registerService)
    {
        _registerService = registerService;
    }
    /// <summary>
    /// 打开新建初始化检查
    /// </summary>
    /// <param name="deviceTypeId"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    [HttpGet]
    [Route("api/register/create/{deviceTypeId}")]
    public async Task<ActionResult> CreateExam(Guid deviceTypeId)
    {
        var result = await _registerService.CreateAsync(deviceTypeId);
        return Ok(result);
    }
    [HttpGet]
    [Route("api/register/{examId}/get-exam")]
    public async Task<ActionResult> GetExam(Guid examId)
    {
        var result = await _registerService.GetExamAsync(examId);
        return Ok(result);
    }
    [HttpGet]
    [Route("api/register/{examId}/get-view")]
    public async Task<ActionResult> GetExamView(Guid examId)
    {
        var result = await _registerService.GetExamViewAsync(examId);
        return Ok(result);
    }
    /// <summary>
    /// 保存检查
    /// </summary>
    /// <param name="examDto"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/save")]
    public async Task<IActionResult> Save([FromForm] ExamDto examDto, [FromForm] string editReason)
    {
        var result = await _registerService.SaveAsync(examDto, editReason);
        return Ok(result);
    }

    /// <summary>
    /// 提交检查
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/submit")]
    public async Task<IActionResult> Submit(Guid examId)
    {
        var result = await _registerService.SubmitAsync(examId);
        return Ok(result);
    }

    /// <summary>
    /// 快速查询检查列表
    /// </summary>
    /// <param name="filterId"></param>
    /// <param name="take"></param>
    /// <param name="skip"></param>
    /// <param name="searchValue"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/get-list")]
    public async Task<IActionResult> GetExamListByFilter([FromBody] DataGridRequest request)
    {
        var result = await _registerService.GetExamListByFilterAsync(request);
        return Ok(result);
    }
    /// <summary>
    /// 删除检查
    /// </summary>
    /// <param name="examIds"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/delete")]
    public async Task<IActionResult> DeleteExam([FromBody] WorkflowInput input)
    {
        var result = await _registerService.DeleteAsync(input, "/api/register/delete");
        return Ok(result);
    }

    /// <summary>
    /// 撤回提交
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/revoke")]
    public async Task<IActionResult> Revoke([FromBody] WorkflowInput input)
    {
        var result = await _registerService.RevokeAsync(input, "/api/register/revoke");
        return Ok(result);
    }

    [HttpPost("api/register/export")]
    public async Task<IActionResult> Export([FromBody] DataGridRequest request)
    {
        var result = await _registerService.ExportAsync(request);
        return Ok(result);
    }

    /// <summary>
    /// 获取高级搜索视图
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("api/register/get-advanced-search-view")]
    public async Task<IActionResult> GetAdvancedSearchView()
    {
        var result = await _registerService.GetAdvancedSearchViewAsync();
        return Ok(result);

    }
    /// <summary>
    /// 挂起
    /// </summary>
    /// <param name="examIds"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/pending")]
    public async Task<IActionResult> Pending([FromBody] Input input)
    {
        var result = await _registerService.PendingAsync(input.ExamIds);
        return Ok(result);
    }
    /// <summary>
    /// 取消挂起
    /// </summary>
    /// <param name="examIds"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/cancel-pending")]
    public async Task<IActionResult> CancelPending([FromBody] Input input)
    {
        var result = await _registerService.CancelPendingAsync(input.ExamIds);
        return Ok(result);
    }
    /// <summary>
    /// 退费
    /// </summary>
    /// <param name="examIds"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/refund")]
    public async Task<IActionResult> Refund([FromBody] WorkflowInput input)
    {
        var result = await _registerService.RefundAsync(input, "/api/register/refund");
        return Ok(result);
    }
    /// <summary>
    /// 设置到检
    /// </summary>
    /// <param name="examIds"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/arrived")]
    public async Task<IActionResult> Arrived([FromBody] WorkflowInput input)
    {
        var result = await _registerService.ArrivedAsync(input, "/api/register/arrived");
        return Ok(result);
    }
    /// <summary>
    /// 置顶
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost("api/register/top")]
    public async Task<IActionResult> Top([FromBody] Input input)
    {
        var result = await _registerService.TopAsync(input.ExamId);
        return Ok(result);
    }
    /// <summary>
    /// 置底
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost("api/register/bottom")]
    public async Task<IActionResult> Bottom([FromBody] Input input)
    {
        var result = await _registerService.BottomAsync(input.ExamId);
        return Ok(result);
    }
    /// <summary>
    /// 置前
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost("api/register/pre")]
    public async Task<IActionResult> Previous([FromBody] Input input)
    {
        var result = await _registerService.PreviousAsync(input.ExamId);
        return Ok(result);
    }
    /// <summary>
    /// 置后
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost("api/register/next")]
    public async Task<IActionResult> Next([FromBody] Input input)
    {
        var result = await _registerService.NextAsync(input.ExamId);
        return Ok(result);
    }
    /// <summary>
    /// 紧急新建
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("api/register/emergency-register")]
    public async Task<IActionResult> EmergencyRegist()
    {
        var result = await _registerService.EmergencyRegisterAsync();
        return Ok(result);
    }
}
